package com.sinodata.bsm.cicp.finder.db2;

import java.io.IOException;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;

import com.sinodata.bsm.cicp.finder.Finder;
import com.sinodata.bsm.cicp.util.DBConnectionUtil;

/**
 * 
 * <p>
 * Description: DB2表空间发现程序
 * 程序返回：表空间的标示，名称，类型。
 * </p>
 *
 * @author wangpeng
 * @version 1.0

 * <p>
 * History: 
 *
 * Date                     Author         Version     Description
 * ---------------------------------------------------------------------------------
 * 2012-6-14 上午10:28:23          wangpeng        1.0         To create
 * </p>
 *
 * @since 
 * @see
 */
public class DB2TableSpaceFinder extends Finder {
    private static final Logger logger = Logger.getLogger(DB2TableSpaceFinder.class);

    @SuppressWarnings("unchecked")
    @Override
    public List<String[]> find(String ip, Map params) {
        List<String[]> list = new ArrayList<String[]>();
        String sql = "select tbsp_id,tbsp_name,tbsp_type from sysibmadm.tbsp_utilization";
        String dbName = (String) params.get("DB");
        Connection conn = null;
        Statement statement = null;
        ResultSet result = null;
        try {
            conn = DBConnectionUtil.getInstance().getConnection("DB2", ip, dbName);
            statement = conn.createStatement();
            result = statement.executeQuery(sql);
            int i = 0;
            while (result.next()) {
                String[] tableSpaceInfo = new String[6];
                list.add(tableSpaceInfo);
                tableSpaceInfo[0] = i + "";
                tableSpaceInfo[1] = result.getString(2);
                tableSpaceInfo[2] = result.getString(2);
                tableSpaceInfo[3] = result.getString(1); //标识
                tableSpaceInfo[4] = result.getString(2); //名称
                tableSpaceInfo[5] = result.getString(3); //类型
                i++;
            }
        } catch (SQLException e) {
            logger.error("failed to exec sql", e);
        } catch (IOException e) {
            logger.error("failed to reader or write", e);
        } finally {
            if (statement != null) {
                try {
                    statement.close();
                } catch (Exception e) {
                    logger.error("failed to close Statement", e);
                }
            }
            DBConnectionUtil.getInstance().returnConnection(conn);
        }
        return list;
    }

}
